#include "regis.h"
#include "ui_regis.h"
#include <QVBoxLayout>
#include <QTimer>
#include <QMessageBox>

extern"C"{
#include"part1.h"
}

regis::regis(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::regis)
{
    ui->setupUi(this);

    connect(ui->regisbbutton, &QPushButton::clicked, this, [=](){

        //转换Qstring为char*, 便于与原始c程序契合。
        const char*file_name = "users.txt";
        QString Q_username = ui->newnameline->text();
        QString Q_password = ui->newpasswrdline->text();
        QString Q_phone = ui->newphoneline->text();
        QByteArray username_utf8 = Q_username.toUtf8();
        QByteArray password_utf8 = Q_password.toUtf8();
        QByteArray phone_utf8 = Q_phone.toUtf8();
        char* username = username_utf8.data();
        char* password = password_utf8.data();
        char* phone = phone_utf8.data();

        int result = usersRegis(username, password, phone, file_name);
        switch(result){
        case 1:
            QMessageBox::warning(this, "Error", "注册失败，该用户名已存在。请重新输入！");
            break;
        case 2:
            QMessageBox::warning(this, "Error", "注册失败，该电话号码已存在。请重新输入！");
            break;
        case 3:
            QMessageBox::warning(this, "Error", "注册失败，文件无法打开");
            break;
        case 5:
            QMessageBox::warning(this, "Error", "注册失败，此电话号码无效。请重新输入！");
            break;
        case 4:
            QMessageBox::information(this, "Success", "注册成功");
            this->close();
            break;
        }
    });
}

regis::~regis()
{
    delete ui;
}

